package br.fbc.facebook;


	

	import java.io.IOException;

	import javax.servlet.Filter;
	import javax.servlet.FilterChain;
	import javax.servlet.FilterConfig;
	import javax.servlet.ServletException;
	import javax.servlet.ServletRequest;
	import javax.servlet.ServletResponse;
	import javax.servlet.http.HttpServletRequest;
	import javax.servlet.http.HttpServletResponse;
	import javax.servlet.http.HttpSession;

	import org.slf4j.Logger;
	import org.slf4j.LoggerFactory;
	import org.slf4j.MDC;
	
	import com.google.code.facebookapi.FacebookException;
	import com.google.code.facebookapi.FacebookWebappHelper;
	import com.google.code.facebookapi.FacebookXmlRestClient;
	import com.google.code.facebookapi.IFacebookRestClient;

	/**
	 * The Facebook User Filter ensures that a Facebook client that pertains to
	 * the logged in user is available in the session object named "facebook.user.client".
	 * 
	 * The session ID is stored as "facebook.user.session". It's important to get
	 * the session ID only when the application actually needs it. The user has to 
	 * authorise to give the application a session key.
	 * 
	 * @author Dave
	 */
	public class FacebookUserFilter implements Filter {
			
		private static String facebookUserId = "facebookUserId";
		private static String ipAddress = "isAddress";
		
	        private static final Logger logger = LoggerFactory.getLogger(FacebookUserFilter.class);
	        
	        private String api_key = "678e8025c5da2bc042c03fcc644570ec";
	        private String secret="789736b7b73873b82e4a29c539219014	";
	        
	        private static final String FACEBOOK_USER_CLIENT = "facebook.user.client";
	        
	        public void init(FilterConfig filterConfig) throws ServletException {
	                //api_key = filterConfig.getServletContext().getInitParameter("facebook_api_key");
	                //secret = filterConfig.getServletContext().getInitParameter("facebook_secret");
	                if(api_key == null || secret == null) {
	                        throw new ServletException("Cannot initialise Facebook User Filter because the " +
	                                                           "facebook_api_key or facebook_secret context init " +
	                                                           "params have not been set. Check that they're there " +
	                                                           "in your servlet context descriptor.");
	                } else {
	                        logger.info("Using facebook API key: " + api_key);
	                }
	        }
	        
	        public void destroy() {
	        }

	        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
	        	
	        	String locale = req.getParameter("fb_sig_locale");
	        	System.out.println("No filter: "+locale);
	        	
	        }

	    public static FacebookXmlRestClient getUserClient(HttpSession session) {
	            return (FacebookXmlRestClient)session.getAttribute(FACEBOOK_USER_CLIENT);
	        }
	}
	

